<template>
  <div class="login_box">
    <div class="login_form">
      <div class="logo_img">
        <img src="../assets/logo.png" alt width="80px" height="80px" />
        <p class="login_title">欢迎登录</p>
      </div>
      <el-form :model="loginInfo" label-width="80px" :rules="rules" ref="loginInfo">
        <div class="login_input">
          <el-form-item label="用户名:" prop="username">
            <el-input prefix-icon="el-icon-user" v-model="loginInfo.username"></el-input>
          </el-form-item>
          <el-form-item label="密码:" prop="password">
            <el-input prefix-icon="el-icon-lock" v-model="loginInfo.password" show-password></el-input>
          </el-form-item>
        </div>
        <div class="login_button">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginInfo">重置</el-button>
          <i class="el-icon-question"><a href="javascript:void(0)">忘记密码</a></i>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginInfo: {
        username: '',
        password: ''
      },
      rules: {
          user: [{
              required: true, message: '请输入用户名', trigger: 'blur'
          }, {
              min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'
          }],
          pwd: [{
              required: true, message: '请输入密码', trigger: 'blur'
          }, {
              min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'
          }]
      }
    }
  },
  methods: {
    // 重置表单
    resetLoginInfo: function () {
        this.loginInfo.username = ''
        this.loginInfo.password = ''
    },
    // 表单登录
    login () {
       this.$refs.loginInfo.validate(async (valid) => {
           if (!valid) return
           const { data: res } = await this.$http.post('login', this.loginInfo)
           if (res.meta.status !== 200) {
             return this.$message({
               message: '登陆失败,请检查用户名和密码',
               type: 'error'
              })
           } else {
             this.$message.success('登陆成功,欢迎您')
           }
           window.sessionStorage.setItem('token', res.data.token)
           this.$router.push('/home')
       })
    }
  }
}
</script>

<style lang="less" scoped>
.login_box {
  background-color: #2b4b6b;
  height: 100%;
}

.login_form {
  background-color: #fff;
  width: 450px;
  height: 300px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 5px;
}
.logo_img {
  height: 90px;
}
.logo_img img {
  margin: 5px;
  box-shadow: 0 0 10px 0 #bbb;
}
.login_title {
  display: inline-block;
  height: 35px;
  font-size: 30px;
  font-weight: 350;
  color: rgb(0, 100, 0);
  position: absolute;
  left: 50%;
  top: 15px;
  transform: translate(-50%);
}
.login_input {
  width: 380px;
  margin-left: 10px;
}
.el-form span {
  text-align: center;
}
.login_button {
  text-align: center;
}
.login_button i{
    margin-left: 10px;
    font-size: 10px;
    position: absolute;
    left: 350px;
}
.login_button a {
    color: black;
    text-decoration: none;
}
.login_button button {
    margin-right: 30px;
}
</style>
